using System;
using System.Text;
using System.Text.RegularExpressions;

namespace ConfigDatabase
{
    /// <summary>
    /// Manages Sql Connection String
    /// http://www.codeproject.com/Articles/8286/Simple-SQL-Connection-String-Manager-Class
    /// </summary>
    public class SqlConnectionString : ICloneable
    {
        private static readonly Regex ParseServer = new Regex(
            @"(?i:((Data\sSource)|(Server)|(Address)|(Addr)|(Network Address))=(?<server>[^;]+))"
            , RegexOptions.Compiled);

        private static readonly Regex ParseAuth = new Regex(
            @"(?i:((Integrated\sSecurity)|(Trusted_Connection))=(?<auth>(true)|(yes)|(sspi)))"
            , RegexOptions.Compiled);

        private static readonly Regex ParseDatabase = new Regex(
            @"(?i:((Initial\sCatalog)|(Database))=(?<database>[^;]+))"
            , RegexOptions.Compiled);

        private static readonly Regex ParsePassword = new Regex(
            @"(?i:((Password)|(Pwd))=(?<password>[^;]+))"
            , RegexOptions.Compiled);

        private static readonly Regex ParseUser = new Regex(
            @"(?i:(User\sID=(?<user>[^;]+)))"
            , RegexOptions.Compiled);

        private string _database = "";
        private string _server = "";
        private bool _auth = true;
        private string _password = "";
        private string _user = "";

        public string ConnectionString
        {
            get
            {
                if (_server.Length == 0 && _database.Length == 0)
                    return "";

                var sb = new StringBuilder();
                if (_server.Length > 0)
                    sb.Append("Server=").Append(_server).Append(";");

                if (_database.Length > 0)
                    sb.Append("Database=").Append(_database).Append(";");

                if (_auth)
                    sb.Append("Integrated Security=SSPI;");

                if (_user.Length > 0)
                    sb.Append("User ID=").Append(_user).Append(";");

                if (_password.Length > 0)
                    sb.Append("Pwd=").Append(_password).Append(";");

                return sb.ToString();
            }
            set
            {
                _server = ParseServer.Match(value).Groups["server"].Value;
                _database = ParseDatabase.Match(value).Groups["database"].Value;
                _auth = _database.Length == 0 ||
                       ParseAuth.Match(value).Groups["auth"].Value.Length > 0;
                _user = ParseUser.Match(value).Groups["user"].Value;
                _password = ParsePassword.Match(value).Groups["password"].Value;
            }
        }

        public string Server
        {
            get { return _server; }
            set { _server = value; }
        }

        public string Database
        {
            get { return _database; }
            set { _database = value; }
        }

        public string UserId
        {
            get { return _user; }
            set { _user = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public bool Authentication
        {
            get { return _auth; }
            set { _auth = value; }
        }

        public bool ValidateConnectionString(string connectString)
        {
            const string regexSplitToParts = "([^=;]*)=([^=;]*)";
            const RegexOptions options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled;
            var reg = new Regex(regexSplitToParts, options);
            return !reg.IsMatch(connectString);
        }

        #region ICloneable Members

        public object Clone()
        {
            var scs = new SqlConnectionString
                          {
                              _auth = _auth,
                              _database = _database,
                              _password = _password,
                              _server = _server,
                              _user = _user
                          };
            return scs;
        }

        #endregion
    }
}